package cn.iocoder.yudao.module.digital.controller.admin.thesis;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.digital.controller.admin.thesis.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.thesis.ThesisDO;
import cn.iocoder.yudao.module.digital.service.thesis.ThesisService;

@Tag(name = "管理后台 - 论文")
@RestController
@RequestMapping("/digital/thesis")
@Validated
public class ThesisController {

    @Resource
    private ThesisService thesisService;

    @PostMapping("/create")
    @Operation(summary = "创建论文")
    @PreAuthorize("@ss.hasPermission('digital:thesis:create')")
    public CommonResult<Integer> createThesis(@Valid @RequestBody ThesisSaveReqVO createReqVO) {
        return success(thesisService.createThesis(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新论文")
    @PreAuthorize("@ss.hasPermission('digital:thesis:update')")
    public CommonResult<Boolean> updateThesis(@Valid @RequestBody ThesisSaveReqVO updateReqVO) {
        thesisService.updateThesis(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除论文")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('digital:thesis:delete')")
    public CommonResult<Boolean> deleteThesis(@RequestParam("id") Integer id) {
        thesisService.deleteThesis(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得论文")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('digital:thesis:query')")
    public CommonResult<ThesisRespVO> getThesis(@RequestParam("id") Integer id) {
        ThesisDO thesis = thesisService.getThesis(id);
        return success(BeanUtils.toBean(thesis, ThesisRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得论文分页")
    @PreAuthorize("@ss.hasPermission('digital:thesis:query')")
    public CommonResult<PageResult<ThesisRespVO>> getThesisPage(@Valid ThesisPageReqVO pageReqVO) {
        PageResult<ThesisDO> pageResult = thesisService.getThesisPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ThesisRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出论文 Excel")
    @PreAuthorize("@ss.hasPermission('digital:thesis:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportThesisExcel(@Valid ThesisPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ThesisDO> list = thesisService.getThesisPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "论文.xls", "数据", ThesisRespVO.class,
                        BeanUtils.toBean(list, ThesisRespVO.class));
    }

}